﻿@using System.Timers
@using Flagrum.Web.Features.AssetExplorer.Data
@using Flagrum.Web.Persistence.Entities
@using Flagrum.Web.Features.AssetExplorer
@using Flagrum.Web.Features.AssetExplorer.Data
@using Flagrum.Web.Features.AssetExplorer.Base
@using Flagrum.Web.Features.AssetExplorer.GameView

@implements Flagrum.Web.Features.AssetExplorer.Base.IAssetExplorerParent

@inject AppStateService AppState
@inject FlagrumDbContext Context
@inject IStringLocalizer<ModelBrowserModal> Localizer

<Modal @ref="Modal" Padding="0" Width="9999" Height="9999" BackgroundClass="bg-grey-900">
    <HeaderView>
        <span class="text-grey-300 flex-grow">@Localizer["BrowseGameModels"]</span>
        <span class="material-icons cursor-pointer" @onclick="() => Modal.Close()">cancel</span>
    </HeaderView>
    <BodyView>
        <div class="w-full h-full flex flex-col">
            <CascadingValue Value="this">
                <AssetExplorer @ref="AssetExplorer" ItemSelectedOverride="OnItemSelected">
                    <AddressBarTemplate>
                        <GameViewAddressBar @ref="AssetExplorer.AddressBar"/>
                    </AddressBarTemplate>
                    <FileListTemplate>
                        <GameViewFileList @ref="AssetExplorer.FileList"/>
                    </FileListTemplate>
                </AssetExplorer>
            </CascadingValue>
        </div>
    </BodyView>
</Modal>

<AlertModal @ref="Alert" />

@code
{
    [CascadingParameter]
    public ModelReplacement Parent { get; set; }

    private Timer _timer;
    private IAssetExplorerNode _currentNode;
    private ModelReplacementPath _path;

    private Modal Modal { get; set; }
    private AssetExplorer AssetExplorer { get; set; }
    public AlertModal Alert { get; set; }

    public AssetExplorerView CurrentView { get; set; }
    private string CurrentUri { get; set; }

    private IAssetExplorerNode CurrentNode
    {
        get => _currentNode;
        set
        {
            _currentNode = value;
            CurrentUri = _currentNode.Path;
        }
    }

    protected override void OnInitialized()
    {
        if (AppState.RootModelBrowserNode == null)
        {
            _timer = new Timer(1000);
            _timer.Elapsed += (_, _) =>
            {
                if (AppState.RootModelBrowserNode != null)
                {
                    _timer.Stop();
                    InvokeAsync(() =>
                    {
                        CurrentNode = AppState.RootModelBrowserNode;
                        StateHasChanged();
                    });
                }
            };

            _timer.Start();
        }
        else
        {
            CurrentNode = AppState.RootModelBrowserNode;
        }
    }
    
    public void CallStateHasChanged() => StateHasChanged();
    
    private void OnItemSelected(IAssetExplorerNode node)
    {
        if (node.Name.EndsWith(".gmdl"))
        {
            _path.Path = node.Path.Replace("data://", "");
            Close();
            Parent.CallStateHasChanged();
            //return;
        }
        
        //CurrentNode = node;
        //StateHasChanged();
    }

    public void Open(ModelReplacementPath path)
    {
        _path = path;
        Modal.Open();
    }

    public void Close()
    {
        Modal.Close();
    }
}